#
#   N 0 T E S :)
#


from simple_term_menu import TerminalMenu
import webbrowser
import os

#---------------------functions-------------------------


def read_config():
    conf = open('.notes_config.txt','r')
    notes = conf.readlines()
    notes = list(map(lambda notes:notes.strip(),notes)) #delete "\n"
    conf.close
    
    return notes


def new_note():
    new_note = str(input("[enter a note]==> "))
    config = open(".notes_config.txt","a")
    new_note = new_note + "\n"
    config.write(new_note)
    config.close()


def view_notes():
    notes = read_config()
    terminal_menu1 = TerminalMenu(notes)
    menu_entry_index = terminal_menu1.show()

    return notes[menu_entry_index]


def get_index():
    notes = read_config()
    terminal_menu1 = TerminalMenu(notes)
    menu_entry_index = terminal_menu1.show()

    return menu_entry_index


def mark_done(done_note,index):
    done_notee = "[*done*] " + str(done_note) + "\n"
    
    with open('.notes_config.txt', 'r') as file: 
	    data = file.readlines() 

    data[int(index)] = done_notee

    with open('.notes_config.txt', 'w') as file: 
	    file.writelines(data) 


def del_note(note_to_del):
    with open(".notes_config.txt", "r") as f:
        lines = f.readlines()
    with open(".notes_config.txt", "w") as f:
        for line in lines:
            if line.strip("\n") != note_to_del:
                f.write(line)


def logo():
    os.system("clear")
    os.system("clear && cat logo | lolcat")
    os.system("echo '                       by chasesun0' | lolcat")


#-------------------------------------------------


logo()

menu = ["[ view notes ] ", "[ make new note ]", "[ mark as done ]", "[ delete note ]", "[ visit my notabug:) ]", "[[ quit ]]"]

terminal_menu0 = TerminalMenu(menu)
menu_entry_index = terminal_menu0.show()

if menu[menu_entry_index] == menu[0]:
    view_notes()

elif menu[menu_entry_index] == menu[1]:
    new_note()
    logo()
    view_notes()

elif menu[menu_entry_index] == menu[2]:
    print("choose a note to mark as done(2 times)")
    mark_as_done = view_notes()
    index = get_index()
    mark_done(mark_as_done,index)
    
    logo()
    view_notes()

elif menu[menu_entry_index] == menu[3]:
    print("choose a note to DELETE")
    note2del = view_notes()
    del_note(note2del)
    
    logo()
    view_notes()

elif menu[menu_entry_index] == menu[4]:
    webbrowser.open("https://notabug.org/chase")

elif menu[menu_entry_index] == menu[5]:
    exit(0)

else:
    print("???")



